package org.sanm.domain.order;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.sanm.common.Result;

import org.sanm.domain.order.model.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
@Tag(name = "订单管理", description = "订单管理")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @PostMapping("add")
    @Operation(summary = "添加订单", description = "添加订单")
    public Result<Order> add(@RequestBody OrderReq orderVo){

        return Result.success(orderService.add(orderVo));
    }

    @PutMapping("update")
    @Operation(summary = "修改订单", description = "修改订单")
    public Result update(@RequestBody OrderReq orderVo){
        orderService.update(orderVo);
        return Result.success("修改成功");
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除订单", description = "删除订单")
    public Result delete(@PathVariable("id") Integer id){
        Db.removeById(id, Order.class);
        Db.lambdaUpdate(OrderGoods.class).eq(OrderGoods::getOrderId, id).remove();
        return Result.success("删除成功");
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "根据id查询订单", description = "根据id查询订单")
    public Result<OrderRes> getDetail(@PathVariable("id") Integer id){

        return Result.success(orderService.getDetail(id));
    }

    @GetMapping("ids")
    @Operation(summary = "根据多个id查询订单", description = "根据多个id查询订单")
    public Result<List<Order>> getByIds(@RequestParam("ids") List<Integer> ids){

        return Result.success();
    }

    @GetMapping("list")
    @Operation(summary = "分页查询订单", description = "分页查询订单")
    public Result<Page<OrderListRes>> page(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "no", required = false) String no,
            @RequestParam(value = "projectName", required = false) String projectName,
            @RequestParam(value = "goodsName", required = false) String goodsName,
            @RequestParam(value = "companyName", required = false) String companyName,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate
    ){


        return Result.success(orderService.list(pageNum, pageSize, no, projectName,goodsName,companyName, startDate, endDate));
    }
}
